<?php

// +----------------------------------------------------------------------
// | author: admin
// +----------------------------------------------------------------------

namespace app\adminapi\logic\travel;


use app\common\enum\YesNoEnum;
use app\common\model\travel\TravelAgency;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * TravelAgency逻辑
 * Class TravelAgencyLogic
 * @package app\adminapi\logic\travel
 */
class TravelAgencyLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/23 18:23
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            TravelAgency::create([
                'name' => $params['name'],
                'licence' => $params['licence'],
                'business_licence' => $params['business_licence'],
                'contact_phone' => $params['contact_phone'],
                'contact_name' => $params['contact_name'],
                'address' => $params['address'],
                'desc' => $params['desc'],
                'brokerage_percent' => $params['brokerage_percent'],
                'status' => $params['status'],
                'sort' => $params['sort']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/23 18:23
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            TravelAgency::where('id', $params['id'])->update([
                'name' => $params['name'],
                'licence' => $params['licence'],
                'business_licence' => $params['business_licence'],
                'contact_phone' => $params['contact_phone'],
                'contact_name' => $params['contact_name'],
                'address' => $params['address'],
                'desc' => $params['desc'],
                'brokerage_percent' => $params['brokerage_percent'],
                'status' => $params['status'],
                'sort' => $params['sort']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/23 18:23
     */
    public static function delete(array $params): bool
    {
        return TravelAgency::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author admin
     * @date 2025/04/23 18:23
     */
    public static function detail($params): array
    {
        return TravelAgency::findOrEmpty($params['id'])->toArray();
    }

    /**
     * @notes  更改状态
     * @param array $params
     * @return bool
     */
    public static function updateStatus(array $params)
    {
        TravelAgency::update([
            'id' => $params['id'],
            'status' => $params['status']
        ]);
        return true;
    }

    /**
     * @notes 文章分类数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAllData()
    {
        return TravelAgency::where(['status' => YesNoEnum::NO])
            ->order(['sort' => 'desc', 'id' => 'desc'])
            ->select()
            ->toArray();
    }
}